<?php $title="离线Thunder"; include 'header.php'; ?>
<form id="xunlei-form">
    <input id="xunlei-url" type="text" placeholder="在这里填入各种链接" style="width:550px"> <input type="submit" value="go" id="xunlei-go" class="btn btn-success">
</form>

<table><tbody id="result-table"></tbody></table>

<script>
    $("#xunlei-form").on("submit", function() {
        var url = $("#xunlei-url").val();
        if (url) {
            $("#result-table").empty().append("<tr><td>loading</td></tr>")
            $.getJSON("http://roybinux.duapp.com/get?callback=?", {url: url},
            function(data) {
                data = data.data;
                if (data.items && data.items.length) {
                    $("#result-table").empty()
                    $.each(data.items, function(n, e) {
                        if (e.url) {
                            $("#result-table").append("<tr><td><a href='"+e.url+"' target='_blank'>"+e.title+"</a></td><td>"+format_size(parseInt(e.size))+"</td></tr>");
                            } else {
                            $("#result-table").append("<tr><td>"+e.title+"</td><td>"+format_size(parseInt(e.size))+"</td></tr>");
                        }
                    });
                    } else {
                    $("#result-table").empty().append("<tr><td>没有找到资源</td></tr>");
                }
                }).error(function() {
                $("#result-table").empty().append("<tr><td>请求出错</td></tr>")
            });
            } else {
            $("#result-table").empty().append("<tr><td>需要url</td></tr>")
        }
        return false;
    });
    function format_size(spare_size)
    {
        if (spare_size == 0)
        return "0B";
        var spare_str;
        var spare_left;
        if( spare_size >= 1024*1024*1024  )
        {
            spare_left = Math.floor(spare_size/(1024*1024*1024)*10);
            spare_str = (spare_left/10).toString()+"GB";
            }else if( spare_size >= 1024*1024 ){
            spare_left = (Math.floor(spare_size*100/(1024*1024)))/100;
            spare_str = spare_left.toString()+"MB";
        }
        else if(spare_size >= 1024){
            spare_left = Math.floor(spare_size/1024);
            spare_str = spare_left.toString()+"KB";
        }
        else{
            spare_str = spare_size + "B";
        }

        return spare_str;
    }
</script>
<?php include 'footer.php'; ?>

